package com.ccrfid.rmc6.resource.business.prison.door;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.business.prison.door.InOutDoorEntity;
import com.ccrfid.rmc6.entity.business.prison.door.InOutRecordDetailEntity;
import com.ccrfid.rmc6.entity.business.prison.door.InOutRecordEntity;
import com.ccrfid.rmc6.pojo.dto.prison.door.InOutCountDto;
import com.ccrfid.rmc6.pojo.dto.prison.door.InOutRecordDto;
import com.ccrfid.rmc6.pojo.dto.prison.door.InOutTypeDto;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.business.prison.door.DoorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Path("in-out-doors")
@Produces(MediaType.APPLICATION_JSON)
public class DoorResource {

    @Autowired
    private DoorService doorService;

    //获取所有二道门列表
    @GET
    @Path("all")
    public List<InOutDoorEntity> getAllDoors() {
        return doorService.getAllDoors();
    }

    //获取单个二道门信息
    @GET
    @Path("{doorId}")
    public InOutDoorEntity getDoorById(@PathParam("doorId") Long doorId) {
        return doorService.getDoorById(doorId);
    }

    //新增二道门信息
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public InOutDoorEntity createDoor(InOutDoorEntity doorEntity) throws AppException {
        return doorService.createDoor(doorEntity);
    }

    //修改二道门信息
    @PUT
    @Path("{doorId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public InOutDoorEntity updateDoor(@PathParam("doorId") Long doorId, InOutDoorEntity doorEntity) throws AppException {
        return doorService.updateDoor(doorId, doorEntity);
    }

    //删除二道门信息
    @DELETE
    @Path("{doorId}")
    public Boolean deleteDoorById(@PathParam("doorId") Long doorId) throws AppException {
        doorService.deleteDoorById(doorId);
        return true;
    }

    //获取某个二道门带出带入记录
    @GET
    @Path("{doorId}/in-out-records")
    public Page<InOutRecordEntity> getInOutRecords(@PathParam("doorId") Long doorId,
                                                   @QueryParam("type") String type,
                                                   @QueryParam("policeName") String policeName,
                                                   @QueryParam("startTime") Timestamp startTime,
                                                   @QueryParam("stopTime") Timestamp stopTime,
                                                   @QueryParam("remark") String remark,
                                                   @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (doorId != null) {
            criteria.add(new Criterion("doorId", Criterion.Operator.EQ, doorId));
        }
        if (type != null) {
            criteria.add(new Criterion("type", Criterion.Operator.EQ, type));
        }
        if (policeName != null) {
            criteria.add(new Criterion("policeName", Criterion.Operator.LIKE, '%' + policeName + '%'));
        }
        if (startTime != null) {
            criteria.add(new Criterion("startTime", Criterion.Operator.GTE, startTime));
        }
        if (stopTime != null) {
            criteria.add(new Criterion("startTime", Criterion.Operator.LTE, stopTime));
        }
        if (remark != null) {
            criteria.add(new Criterion("remark", Criterion.Operator.EQ, remark));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return doorService.getInOutRecords(criteria, pageRequest);
    }

    //获取某个带入带出记录详细信息
    @GET
    @Path("{doorId}/in-out-records/{recordId}/detail")
    public Page<InOutRecordDetailEntity> getInOutRecordDetail(@PathParam("recordId") Long recordId,
                                                              @QueryParam("personnelName") String personnelName,
                                                              @QueryParam("cardNumber") Long cardNumber,
                                                              @QueryParam("startTime") Timestamp startTime,
                                                              @QueryParam("stopTime") Timestamp stopTime,
                                                              @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (recordId != null) {
            criteria.add(new Criterion("recordId", Criterion.Operator.EQ, recordId));
        }
        if (personnelName != null) {
            criteria.add(new Criterion("personnelName", Criterion.Operator.LIKE, '%' + personnelName + '%'));
        }
        if (cardNumber != null) {
            criteria.add(new Criterion("cardNumber", Criterion.Operator.EQ, cardNumber));
        }
        if (startTime != null) {
            criteria.add(new Criterion("recordTime", Criterion.Operator.GTE, startTime));
        }
        if (stopTime != null) {
            criteria.add(new Criterion("recordTime", Criterion.Operator.LTE, stopTime));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return doorService.getInOutRecordDetail(criteria, pageRequest);
    }

    //开始一个带入/带出操作
    @POST
    @Path("{doorId}/start-take-{type}")
    @Consumes(MediaType.APPLICATION_JSON)
    public InOutRecordEntity startInOutRecord(@PathParam("doorId") Long doorId, @PathParam("type") String type,
                                              InOutRecordEntity recordEntity) throws AppException {
        return doorService.startInOutRecord(doorId, type,
                recordEntity.getPoliceId(), recordEntity.getPoliceName(), recordEntity.getRemark());
    }

    //某个在押人员带入/离开
    @POST
    @Path("{doorId}/take-{type}-records/{recordId}/detail")
    @Consumes(MediaType.APPLICATION_JSON)
    public InOutRecordDetailEntity createInOutRecordDetail(@PathParam("recordId") Long recordId,
                                                           InOutRecordDetailEntity recordDetailEntity) throws AppException {
        return doorService.createInOutRecordDetail(recordId, recordDetailEntity);
    }

    //结束一个带入/带出操作
    @POST
    @Path("{doorId}/stop-take-{type}/{recordId}")
    public InOutRecordEntity stopInOutRecord(@PathParam("recordId") Long recordId) throws AppException {
        return doorService.stopInOutRecord(recordId);
    }

    //获取带出/带入事由
    @GET
    @Path("types")
    public List<InOutTypeDto> getInOutTypes() {
        List<InOutTypeDto> list = new ArrayList<>();
        list.add(new InOutTypeDto("out", "出工"));
        list.add(new InOutTypeDto("in", "收工"));
        list.add(new InOutTypeDto("out", "会见"));
        list.add(new InOutTypeDto("out", "谈话"));
        list.add(new InOutTypeDto("out", "就医"));
        list.add(new InOutTypeDto("in", "回监"));
        return list;
    }

    @GET
    @Path("count-in-org")
    public InOutCountDto getInOutCountByOrg(@QueryParam("date") String dateStr) {
        return doorService.getInOutCountByOrg(dateStr);
    }

    @POST
    @Path("{doorId}/take-{type}")
    public InOutRecordEntity takeInOut(@PathParam("doorId") Long doorId, @PathParam("type") String type,
                                       InOutRecordDto dto) throws AppException {
        return doorService.takeInOut(doorId, type, dto);
    }
}
